<?php

class Cart_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    public function add($product_id,$amount){
        $cart = $this->session->userdata('cart');
        $product = $this->get_product($product_id);
        if(empty($cart)){
            //cart still empty
            $cart = array();
            $cart[$product_id] = array();
            $cart[$product_id]['amount'] = $amount;
        }else{
            if(array_key_exists($product_id,$cart)){
                $cart[$product_id]['amount'] += $amount;
            }else{
                $cart[$product_id] = array();
                $cart[$product_id]['amount'] = $amount;
            }
        }
        $cart[$product_id]['price'] = $product->price;
        $cart[$product_id]['name'] = $product->name;
        $this->session->set_userdata('cart',$cart);
    }
    
    public function get(){
        $result = $this->session->userdata('cart');
        return ($result)?$result:array();
    }
    
    public function delete($product_id){
        $cart = $this->session->userdata('cart');
        if(!empty($cart)){
            $current_cart = $cart[$product_id];
            if(!empty($current_cart)){
                unset($cart[$product_id]);
            }
        }
        $this->session->set_userdata('cart',$cart);
    }
    
    public function clear(){
        $this->session->set_userdata('cart',array());
    }

    //REGISTRATION
    public function add_purchase($input){
        $this->db->set('first_name',$input['first_name']);
        $this->db->set('last_name',$input['last_name']);
        $this->db->set('address',$input['address']);
        $this->db->set('city',$input['city']);
        $this->db->set('state',$input['state']);
        $this->db->set('country',$input['country']);
        $this->db->set('postal',$input['postal']);
        $this->db->set('phone',$input['phone']);
        $this->db->set('email',$input['email']);
        $this->db->set('additional',$input['additional']);
        //shipping
        $this->db->set('first_name_s',$input['first_name_s']);
        $this->db->set('last_name_s',$input['last_name_s']);
        $this->db->set('address_s',$input['address_s']);
        $this->db->set('city_s',$input['city_s']);
        $this->db->set('state_s',$input['state_s']);
        $this->db->set('country_s',$input['country_s']);
        $this->db->set('postal_s',$input['postal_s']);
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->insert('purchase');
        $purchase_id = $this->db->insert_id();
        $this->add_purchase_item($purchase_id);
    }
    
    public function add_purchase_item($purchase_id){
        $cart = $this->get();
        foreach($cart as $key=>$c){
            $this->insert_purchase_item($purchase_id, $key, $c['amount'], $c['price']);
        }
    }
    
    public function insert_purchase_item($purchase_id,$product_id,$quantity,$price){
        $this->db->set('purchase_id',$purchase_id,FALSE);
        $this->db->set('product_id',$product_id,FALSE);
        $this->db->set('quantity',$quantity,FALSE);
        $this->db->set('price',$price,FALSE);
        $this->db->insert('purchase_item');
    }


    //additional
    public function get_product($id){
        $this->db->where('id',$id);
        $p = $this->db->get('products');
        return $p->first_row();
    }
}